// 引入SDK核心类，js文件根据自己业务，位置可自行放置
const QQMapWX = require('../libs/qqmap-wx-jssdk.min.js');
// let qqmapsdk;
// 实例化API核心类
const qqmapsdk = new QQMapWX({
    key: 'B5VBZ-2273F-TS7JK-NH62Y-L4B3H-VIFXD'
});
// 存
export const initCurrentLocation = () => {
    //位置坐标，默认获取当前位置，非必须参数
   qqmapsdk.reverseGeocoder({
    success(res){
      wx.setStorageSync('userMAPCurrentLocation',res.result.ad_info)
      //  wx.setStorage({key:'userMAPLocation',data:res.result['ad_info']})     
    }
  })
}
// 取
export const getCurrentLocation = () => {
  if(!wx.getStorageSync('userMAPCurrentLocation')){
    initCurrentLocation()
  } 
  return wx.getStorageSync('userMAPCurrentLocation')  
}
// 省份
export  const initProvincesAndCities = () =>{
  qqmapsdk.getCityList({
    success: function(res) {//成功后的回调
      console.log(res);
      wx.setStorage({
        key:'provinces',
        data:res.result[0]
      })
      wx.setStorage({
        key:'cities',
        data:res.result[1]
      })
    },
  });
}
// 分市
export const getqiforEachProvincesAndCities = () =>{
  let allcities = wx.getStorageSync('cities')
  let  allcitiesgoodsarr = []
  let index = 0;
  let indexitem;
  allcities.forEach(item =>{
    if(allcitiesgoodsarr.length ===0) {
      allcitiesgoodsarr.push([item])
    } else {
      if(item.id.slice(0,2) === indexitem.id.slice(0,2)) {
        allcitiesgoodsarr[index].push(item);
      } else {
        allcitiesgoodsarr.push([item])
        index++
      }
    }
    indexitem = item;
  });
  let allprovinces = wx.getStorageSync('provinces')
  let tree = [];
  allprovinces.forEach((item,index) =>{
    item['childrens'] = allcitiesgoodsarr[index]
      tree.push(item)
  })
return tree;

}
